﻿//---------------------------------------------------------------------------

#ifndef h_globalH
#define h_globalH
//---------------------------------------------------------------------------

class HThread : public TThread
{

public :

   __fastcall HThread(bool CreateSuspended)
      : TThread(CreateSuspended)
   {
      FExitEvent = new TSimpleEvent(false);
   }
   virtual __fastcall ~HThread(void)
   {
      Exit();
      WaitFor();
      delete FExitEvent;
   }
   void __fastcall Exit(void)
   {
      Terminate();
      FExitEvent->SetEvent();
   }

protected :

   TSimpleEvent *FExitEvent;
};

class HComm
{
public :

// 返回 -1 ：其他错误，需要检查 ErrCode
// 返回 -2 ：超时
// 返回 -3 ：NumOfEvent 超过 8
// 返回 -4 ：对方关闭
// 返回 -5 ：内部故障

   static void __fastcall Init(void);
   static void __fastcall Close(void);
   
   static int __fastcall Accept(SOCKET Sock, SOCKET &MsgSock,
      string &IpAddr, int TimeOut, DWORD *ErrCode = NULL,
      const HANDLE *EventHandles = NULL, int NumOfEvent = 0);

   static int __fastcall Connect(SOCKET Sock, const char *IpAddr,
      int Port, int TimeOut, DWORD *ErrCode = NULL,
      const HANDLE *EventHandles = NULL, int NumOfEvent = 0);

   static int __fastcall Read(SOCKET Sock, void *Buf, int Len,
      int TimeOut, DWORD *ErrCode = NULL,
      const HANDLE *EventHandles = NULL, int NumOfEvent = 0);

   static int __fastcall Write(SOCKET Sock, const void *Buf, int Len,
      int TimeOut, DWORD *ErrCode = NULL,
      const HANDLE *EventHandles = NULL, int NumOfEvent = 0);

   static int __fastcall ReadByLength(SOCKET Sock, void *Buf, int &Len,
      int TimeOut, DWORD *ErrCode = NULL,
      const HANDLE *EventHandles = NULL, int NumOfEvent = 0);

   static int __fastcall WriteByLength(SOCKET Sock, const void *Buf, int Len,
      int TimeOut, DWORD *ErrCode = NULL,
      const HANDLE *EventHandles = NULL, int NumOfEvent = 0);
};

struct HColumnInfo
{
   UTF8String Name;
   UTF8String Type;
   int Length;
   int Scale;
   bool IsKey;
} ;

struct HServerInfo
{
   UTF8String Name;
   UTF8String IpAddr;
   UTF8String Port;
   UTF8String Protocol;
};

struct HJobListInfo
{
   UTF8String Name;
   UTF8String Command;
   UTF8String CommandUpper;
   UTF8String TagUpper;
};

bool __fastcall CheckExpr(const UTF8String &Expr, UTF8String &ErrorString);
void __fastcall SetStringToMemo(TMemo *Memo, const UTF8String &String);
UTF8String __fastcall GetStringFromMemo(TMemo *Memo);
bool __fastcall FileExist(const UTF8String &FileName);
void __fastcall IntToFontStyle(int Int,
   Set<TFontStyle, fsBold, fsStrikeOut> &Style);
void __fastcall FontStyleToInt(int &Int,
   const Set<TFontStyle, fsBold, fsStrikeOut> &Style);
void __fastcall CreateSpecialDateList(TComboBox *ComboBox,
   const vector<string> &Dates, bool IndFlag = false);

template<typename V, typename T>
   bool find_name(const V &Vector, const T &Name, int &i)
{
   for( i = 0; i < Vector.size(); i++ )
   {
      if( Vector[i].Name == Name )
         break;
   }
   if( i == Vector.size() )
      return false;
   else
      return true;
}

#define VERSION_STRING "2.9.0"
const int VER_THIS = 0x0209;

#endif
